import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("Notification"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          child: NotificationListener(
              onNotification: (notification) {
                print("-->${notification.runtimeType}");
                switch (notification.runtimeType) {
                  case MyNotification:
                    print((notification as MyNotification).msg);
                    break;
                }
              },
              child: ListView(
                children: <Widget>[
                  Container(
                    color: Colors.cyan,
                    width: double.infinity,
                    height: 900,
                  ),
                  Container(
                    color: Colors.red,
                    width: double.infinity,
                    height: 900,
                  ),
                  /*RaisedButton( // context 不是根context 收不到通知
                    child: Text("Click me"),
                    onPressed: () {
                        print("pressed");
                        MyNotification(msg: "--->>>---sended--->>").dispatch(context);
                    },
                  )*/
                  Builder(builder: (context) {
                    return RaisedButton(
                        child: Text("Click"),
                        onPressed: () {
                          print("pressed");
                          MyNotification(msg: "--->>>---sended--->>")
                              .dispatch(context);
                        });
                  })
                ],
              )),
        ),
      ],
    );
  }
}

class MyNotification extends Notification {
  String msg;

  MyNotification({this.msg});
}
